//
//  MoAlertQue.swift
//  dycommon_Example
//
//  Created by zhuxietong on 2021/2/3.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import Eelay



open class MoAlert: NSObject{
    static var _share:MoAlert? = nil
    public static var share:MoAlert{
        get{
            guard let share = _share else {
                let share = MoAlert()
                _share = share
                return share
            }
            return share
        }
    }
 
    public func clear() {
        queue.clear()
    }
    public func reset() {
        queue.reset()
        alerts = []
    }

    
    open var container:UIViewController?{
           get{
            if let v = controller{
                return v
            }
            
            var back:UIViewController? = nil
            if back == nil{
                back = UIApplication.shared.keyWindow?.rootViewController
            }
            if back == nil{
                back = __nav
            }
            return back
        }
    }
       
  
        
    public override init() {
        super.init()
        initStatus()
    }
    
    func initStatus() {
//
        queue.notaskTip = {[weak self] in
            __co_delay(delay: 0.28) {
                self?.alerts = []
                print("finisheddd")
            }
        }
        queue.uiDelayJust = (0,0,0)
        queue.maxOperationCount = 1
        queue.observation = {[weak self]
            action in
            guard let ws = self else {
                return
            }
            switch action {
            case .end(task: let task):
                ws.closed(alert: task )
            case .start(task: let task):
                ws.showAlert(alert: task )
            }
        }
    }
    

    
    func showAlert(alert:MoAlertController) {
        alert.show(at: self.container)

//        co_delay(delay: 0.3) {[weak self] in
//            guard let `self` = self else{return}
//            alert.show(at: self.controller)
//
//        }
    }
    
    func closed(alert:MoAlertController) {
        var n:Int? = nil
        var i = 0
        for t in alerts {
            if t === alert{
                n = i
            }
            i = i + 1
        }
        if let index = n{
            alerts.remove(at: index)
        }
    }
    
  
    var queue = MoQueue<MoAlertController>()
    
    
    public weak var controller:UIViewController? = nil
    public init(at controller:UIViewController?) {
        super.init()
        self.controller = controller
    }
 
    public var alerts:[MoAlertController] = []
    
    @discardableResult
    public func append(alert:MoAlertController) ->MoAlert{
        alerts.append(alert)
        queue.append(task: alert)
        alert.tellClose = { ctr in
            co_delay(delay: 0.4) {
                ctr.end()
            }
        }
        return self
    }
    
    
        
//    public func `do`(mode:Mode,id:String?=nil,time:MoTimeInterval?=nil) {
//        addToWindow()
//        let tip = Tip(mode: mode, time: time ?? .default)
//        tip.ID = id
//        queue.append(task: tip)
//    }
    
}
